<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
Some known package-level variable initialization order bugs in certain Go toolchain versions
</h1>
<p></p>
<h2 class="tmd-header-2">
Case 1
</h2>
<p></p>
<div class="tmd-usual">
Due to <a href="https://github.com/golang/go/issues/66585">a function inline bug</a> (the function call <code class="tmd-code-span">foo()</code> is inlined here), with Go toolchain versions prior to v1.23, the following program prints <code class="tmd-code-span">1 0</code>, but it should print <code class="tmd-code-span">1 1</code> by <a href="https://go101.org/article/evaluation-orders.html">the rules</a> specified in Go specification.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
The bug has been fixed since Go toolchain v1.23.
</div>
<p></p>
<pre class="tmd-code">
<code class="language-Go">package main

var x = 0
var a = foo()
var b = x

func foo() int {
	x++
	return x
}

func main() {
	println(a, b)
}
</code></pre>
<p></p>
<h2 class="tmd-header-2">
Case 2
</h2>
<p></p>
<div class="tmd-usual">
Due to <a href="https://github.com/golang/go/issues/51913">a compiler optimization bug</a>, with Go toolchain versions prior to v1.22, the following program prints <code class="tmd-code-span">true</code>, but it should print <code class="tmd-code-span">false</code> by <a href="https://go101.org/article/evaluation-orders.html">the rules</a> specified in Go specification.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
The bug has been fixed since Go toolchain v1.22.
</div>
<p></p>
<pre class="tmd-code">
<code class="language-Go">package main

var _ = func() int {
	a = false
	return 0
}()

var a = true
var b = a

func main() {
	println(b)
}
</code></pre>
<p></p>
<h2 class="tmd-header-2">
Case 3
</h2>
<p></p>
<div class="tmd-usual">
Due to <a href="https://github.com/golang/go/issues/22326">a compiler bug</a>, with Go toolchain versions prior to v1.12, the following program prints <code class="tmd-code-span">5 4</code>, but it should print <code class="tmd-code-span">4 5</code> by <a href="https://go101.org/article/evaluation-orders.html">the rules</a> specified in Go specification.
</div>
<p></p>
<p></p>
<div class="tmd-usual">
The bug has been fixed since Go toolchain v1.13.
</div>
<p></p>
<pre class="tmd-code">
<code class="language-Go">package main

var (
  a = c
  b = f()
  c = f()
  d = 3
)

func f() int {
  d++
  return d
}

func main() {
	println(b, c)
}
</code></pre>
</div>
